import { asyncRoutes, constantRoutes } from "@/router";
import apis from "@/api/myApi/api.js";
import _import from "@/router/modules/_import_production";
import Layout from "@/layout";
import { MessageBox, Message } from "element-ui";
// import { delete } from "vue/types/umd";
function _imp(name) {
  return function() {
    return import(`@/views${name}`);
  };
}
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role));
  } else {
    return true;
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = [];

  routes.forEach(route => {
    const tmp = { ...route };
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles);
      }
      res.push(tmp);
    }
  });

  return res;
}

const state = {
  routes: [],
  addRoutes: []
};
const loadView = view => {
  return resolve => require([`@/views${view}`], resolve);
};
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  }
};
const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise((resolve, reject) => {
      apis.getPermissionInfo().then(function(res) {
        console.log(res, "修改之后的");
        if (res.data.data.length == 0) {
          Message({
            message: `当前用户没有权限`,
            type: "error"
          });
          reject();
        } else {
          let comppn = res.data.data[0].children[0].path;
          // let newComppn = comppn.replace("/index.vue", "");
          res.data.data.forEach(item => {
            delete item.createTime;
            item.component = { render: c => c("router-view") };
            item.children.forEach(ele => {
              ele.component = loadView(ele.component);
            });
          });
          let accessedRoutes;
          if (roles.includes("admin")) {
            accessedRoutes = res.data.data || [];
            // accessedRoutes = filterAsyncRoutes(res.data.data, roles);
          } else {
            accessedRoutes = filterAsyncRoutes(res.data.data, roles);
          }
          let newArr = [];

          console.log(accessedRoutes);
          let arr = [
            {
              path: "/",
              component: Layout,
              name: "admin",
              meta: {
                title: "信阳公交后台",
                // icon: "logo",
                roles: ["admin"]
              },
              children: [...accessedRoutes],
              redirect: comppn
            },
            { path: "*", redirect: "/404", hidden: true }
          ];
          // arr.push();
          commit("SET_ROUTES", arr);
          resolve(arr);
        }
      });
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
